package com.pilipili.zeus.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pilipili.zeus.eduService.client.VodClient;
import com.pilipili.zeus.eduService.entity.EduVideo;
import com.pilipili.zeus.eduService.entity.excel.VideoExcelData;
import com.pilipili.zeus.eduService.listener.VideoListener;
import com.pilipili.zeus.eduService.mapper.EduVideoMapper;
import com.pilipili.zeus.eduService.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 番剧视频 服务实现类
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-26
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    /**
     * 根据番剧ID删除小节和对应视频
     * 优化：集合为空不能添加到删除列表，列表为空不执行删除视频
     * @param courseId
     */
    @Autowired
    VodClient vodClient;
    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_Id",courseId);
        List<EduVideo> videoList = baseMapper.selectList(queryWrapper);
        List<String> videos = new ArrayList<>();
        for (EduVideo video : videoList) {
            if(!StringUtils.isEmpty(videoList)){
                videos.add(video.getVideoSourceId());
            }
        }
        if(videos.size()>0){
            vodClient.deleteVideos(videos);
        }
        QueryWrapper<EduVideo> query = new QueryWrapper<>();
        queryWrapper.eq("course_Id",courseId);
        baseMapper.delete(query);
    }

    @Override
    public void saveVideosDataByExcel(MultipartFile file, EduVideoService videoService) {
        try {
            //文件输入流,调用方法读取Excel
            InputStream stream = file.getInputStream();
            EasyExcel.read(stream, VideoExcelData.class,new VideoListener(videoService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
